<?php
/**
 * User:ayou chow
 * Date: 2019-11-26
 * Time: 10:42
 */

namespace app\admin\controller;


use app\common\model\GoldPriceModel;
use think\facade\Request;
use think\Validate;

class GoldController extends BaseController {
    public function index() {
        return $this->fetch();
    }

    public function add() {
        return $this->fetch();
    }

    public function edit() {
        $id       = Request::param('id');
        $goldinfo = GoldPriceModel::get($id);
        $goldinfo->price = bcdiv(strval($goldinfo->price), '100', 0 );
        $goldinfo->gold = bcdiv(strval($goldinfo->gold), '100', 0 );
        $goldinfo->gold_added = bcdiv(strval($goldinfo->gold_added), '100', 0 );
        $this->assign("goldinfo", $goldinfo);
        return $this->fetch();
    }

    public function getlist() {
        $golds = GoldPriceModel::order("price", "asc")->all();
        // 遍历 $golds，将每个元素的 price 字段除以 100
        foreach ($golds as &$gold) {
            $gold->price = bcdiv(strval($gold->price), '100', 2);
            $gold->gold = bcdiv(strval($gold->gold), '100', 2);
            $gold->gold_ios = bcdiv(strval($gold->gold_ios), '100', 2);
            $gold->gold_added = bcdiv(strval($gold->gold_added), '100', 2);
        }
        if (count($golds) > 0) {
            return self::bulidSuccess($golds);
        } else {
            return self::bulidFail("未查询到相关数据");
        }
    }

    public function add_post() {
        $all_param = Request::param();

        $validate = new Validate([
            'gold'    => 'require|integer|egt:0',
            'gold_added' => 'require|integer|egt:0',
            'price' => 'require|integer|egt:0',
        ], [
            "gold.require"    => "请输入宝石数量",
            "gold.integer"  => "宝石数量必须为整数",
            "gold.egt"  => "宝石数量不能小于0",
            "gold_added.require" => "请输入赠送个数",
            "gold_added.integer"  => "赠送个数必须为整数",
            "gold_added.egt"  => "赠送个数不能小于0",
            "price.length"  => "请输入价格",
            "price.integer"  => "价格必须为整数",
            "price.egt"  => "价格不能小于0",
        ]);
        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }


        $goldPriceModel = new GoldPriceModel($all_param);
        $goldPriceModel->price *= 100; 
        $goldPriceModel->gold *= 100; 
        $goldPriceModel->gold_added *= 100; 
        if ($goldPriceModel->save()) {
            // 记录日志
            $log_content = "添加金币配置，ID：{$goldPriceModel->id}，购买个数：{$goldPriceModel->gold}，赠送个数：{$goldPriceModel->gold_added}，价格：{$goldPriceModel->price}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        } else {
            return self::bulidFail();
        }
    }

    public function edit_post() {
        $all_param = Request::param();

        $validate = new Validate([
            'gold'    => 'require|integer|egt:0',
            'gold_added' => 'require|integer|egt:0',
            'price' => 'require|integer|egt:0',
        ], [
            "gold.require"    => "请输入宝石数量",
            "gold.integer"  => "宝石数量必须为整数",
            "gold.egt"  => "宝石数量不能小于0",
            "gold_added.require" => "请输入赠送个数",
            "gold_added.integer"  => "赠送个数必须为整数",
            "gold_added.egt"  => "赠送个数不能小于0",
            "price.length"  => "请输入价格",
            "price.integer"  => "价格必须为整数",
            "price.egt"  => "价格不能小于0",
        ]);
        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }

        $goldPriceModel = GoldPriceModel::get(Request::param('id'));
        if (!$goldPriceModel) {
            return self::bulidFail("条目不存在");
        }
        $all_param["price"] *= 100;
        $all_param["gold"] *= 100; 
        $all_param["gold_added"] *= 100; 
        if ($goldPriceModel->save($all_param)) {
            // 记录日志
            $log_content = "编辑金币配置，ID：{$goldPriceModel->id}，购买个数：{$goldPriceModel->gold}，赠送个数：{$goldPriceModel->gold_added}，价格：{$goldPriceModel->price}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        } else {
            return self::bulidFail();
        }
    }

    public function del_post() {
        $id             = Request::param('id');
        $goldPriceModel = GoldPriceModel::get($id);
        if (!$goldPriceModel) {
            return self::bulidFail("条目不存在");
        }

        if (GoldPriceModel::destroy($id)) {
            // 记录日志
            $log_content = "删除金币配置，ID：{$goldPriceModel->id}，购买个数：{$goldPriceModel->gold}，赠送个数：{$goldPriceModel->gold_added}，价格：{$goldPriceModel->price}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess([]);
        } else {
            return self::bulidFail();
        }
    }
}